﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer.Manager;
using Objects;
using System.Text;
using Common;
using System.Web.Services;
using AutoFix.Base;
using System.Web.Script.Serialization;
namespace AutoFix
{
    public partial class edit_article : BasePage
    {
        #region [Base Implementations]
        /// <summary>
        /// Gets or sets a value indicating whether [is login page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is login page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsLoginPage
        {
            get { return false; }
        }
        /// <summary>
        /// Gets or sets a value indicating whether [is anonymous page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is anonymous page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsAnonymousPage
        {
            get { return false; }
        }
        /// <summary>
        /// Page name
        /// </summary>
        protected override string PageName
        {
            get { return "Edit Article"; }
        }

        /// <summary>
        /// Gets the authorized user types.
        /// </summary>
        protected override List<UserType> AuthorizedUserTypes
        {
            get { return new List<UserType>(new UserType[] { UserType.Admin, UserType.Contributor }); }
        }
        #endregion      

        #region [Event Handlers]
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            ArticleManager articleManager = new ArticleManager();
            List<Article> articles = null;
            try
            {
                if (Request.QueryString["article"] != null)
                {
                    int articleID = Convert.ToInt32(Request.QueryString["article"]);
                    articles = articleManager.GetArticles(articleID);
                    if (articles != null && articles.Count > 0)
                    {
                        if (articles[0].IsActive != false)
                        {
                            this.txtArticleName.Text = articles[0].ArticleName;
                            LoadImage(articles[0].ArticleImageUrl, articles[0].ArticleID);
                            LoadVehicleDetails(articles[0].ArticleID);
                            this.hdnArticleTagID.Value = articles[0].ArticleID.ToString();
                            LoadArticleType(articles[0].ArticleID);
                        }
                    }
                }
            }
            catch (Exception)
            {

                this.pageActionMessage.InnerText = "Failed to load article.";
                this.pageActionMessage.Attributes.Add("class", "error-msg");
            }
            finally
            {
                articleManager = null;
                articles = null;
            }
        }
        /// <summary>
        /// Handles the click event of the edit steps button.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnStepList_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["article"] != null)
            {
                int articleID = Convert.ToInt32(Request.QueryString["article"]);
                Response.Redirect("step_list.aspx?article=" + articleID, true);

            }
        }
        #endregion      

        #region [Web Methods]
        /// <summary>
        /// Gets the model type years
        /// </summary>
        /// <param name="vehicleModelTypeID">vehicleModelTypeID</param>
        /// <returns>serialized model type years</returns>
        [WebMethod]
        public static string GetModelTypeYears(string vehicleModelTypeID)
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            List<KeyValuePair<string, string>> resultList = new List<KeyValuePair<string, string>>();
            VehicleModelTypeYearManager vehicleModelTypeYearManager = new VehicleModelTypeYearManager();
            List<VehicleModelTypeYear> vehicleModelTypeYears = null;
            string returnedString = string.Empty;

            try
            {
                vehicleModelTypeYears = vehicleModelTypeYearManager.GetVehicleModelTypeYears();
                foreach (VehicleModelTypeYear vehicleModelTypeYear in vehicleModelTypeYears)
                {
                    if (Convert.ToInt16(vehicleModelTypeID) == vehicleModelTypeYear.VehicleModelTypeYearID)
                    {
                        resultList.Add(new KeyValuePair<string, string>(vehicleModelTypeYear.VehicleModelTypeYearID.ToString(), vehicleModelTypeYear.VehicleYear.ToString()));

                    }

                }

                returnedString = jsonSerialize.Serialize(resultList);

            }
            catch
            {

            }
            finally
            {
                jsonSerialize = null;
                resultList = null;
                vehicleModelTypeYearManager = null;
                vehicleModelTypeYears = null;
            }
            return returnedString;
        }
        /// <summary>
        /// Gets the model type
        /// </summary>
        /// <param name="vehicleModelID">vehicleModelID</param>
        /// <returns>serialized model types</returns>
        [WebMethod]
        public static string GetModelTypes(string vehicleModelID)
        {

            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            List<KeyValuePair<string, string>> resultList = new List<KeyValuePair<string, string>>();
            VehicleModelTypeManager vehicleModelTypeManager = new VehicleModelTypeManager();
            List<VehicleModelType> vehicleModelTypes = null;
            string returnedString = string.Empty;

            try
            {
                vehicleModelTypes = vehicleModelTypeManager.GetVehicleModelTypes();
                foreach (VehicleModelType vehicleModelType in vehicleModelTypes)
                {
                    if (Convert.ToInt16(vehicleModelID) == vehicleModelType.VehicleModelID)
                    {
                        resultList.Add(new KeyValuePair<string, string>(vehicleModelType.VehicleModelTypeID.ToString(), vehicleModelType.VehicleModelTypeName));

                    }

                }

                returnedString = jsonSerialize.Serialize(resultList);

            }
            catch
            {

            }
            finally
            {
                jsonSerialize = null;
                resultList = null;
                vehicleModelTypeManager = null;
                vehicleModelTypes = null;
            }
            return returnedString;
        }
        /// <summary>
        /// Gets the models
        /// </summary>
        /// <returns>serialized models</returns>
        [WebMethod]
        public static string GetAllModels()
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            List<KeyValuePair<string, string>> resultList = new List<KeyValuePair<string, string>>();
            VehicleModelManager vehicleModelManager = new VehicleModelManager();
            List<VehicleModel> vehicleModels = null;
            string returnedString = string.Empty;

            try
            {
                vehicleModels = vehicleModelManager.GetVehicleModels();
                foreach (VehicleModel vehicleModel in vehicleModels)
                {
                    resultList.Add(new KeyValuePair<string, string>(vehicleModel.VehicleModelID.ToString(), vehicleModel.VehicleModelName));
                }

                returnedString = jsonSerialize.Serialize(resultList);

            }
            catch
            {
            }
            finally
            {
                jsonSerialize = null;
                resultList = null;
                vehicleModelManager = null;
                vehicleModels = null;
            }
            return returnedString;
        }
        /// <summary>
        /// Gets the tags
        /// </summary>
        /// <returns>serialized tags</returns>
        [WebMethod]
        public static string GetTags()
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            List<KeyValuePair<int, string>> resultList = new List<KeyValuePair<int, string>>();
            TagManager tagManager = new TagManager();
            List<Tag> tags = null;
            string returnedString = string.Empty;

            try
            {
                tags = tagManager.GetTags();
                foreach (Tag tag in tags)
                {
                    resultList.Add(new KeyValuePair<int, string>(tag.TagID, tag.TagName));
                }

                returnedString = jsonSerialize.Serialize(resultList);

            }
            catch
            {
            }
            finally
            {
                jsonSerialize = null;
                resultList = null;
                tagManager = null;
                tags = null;
            }
            return returnedString;


        }
        /// <summary>
        /// Gets the article tags by article id
        /// </summary>
        /// <returns>serialized tags</returns>
        [WebMethod]
        public static string GetArticleTags(int articleID)
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            List<KeyValuePair<int, string>> resultList = new List<KeyValuePair<int, string>>();
            ArticleTagManager tagManager = new ArticleTagManager();
            List<GetTagByArticleID> articleTags = null;
            int counter = 0;
            string returnedString = string.Empty;
            try
            {
                articleTags = tagManager.GetTagByArticleID(articleID);
                if (articleTags != null && articleTags.Count > 0)
                {
                    foreach (GetTagByArticleID articleTag in articleTags)
                    {
                        counter = counter + 1;
                        resultList.Add(new KeyValuePair<int, string>(counter, articleTag.Tag.ToString()));
                    }

                    returnedString = jsonSerialize.Serialize(resultList);
                }

            }
            catch 
            {
            }
            finally
            {
                jsonSerialize = null;
                resultList = null;
                tagManager = null;
                articleTags = null;
            }

            return returnedString;
        }
        #endregion

        #region [Private Methods]
        /// <summary>
        /// Loads the image in page
        /// </summary>
        /// <param name="articleImageURL">articleImageURL</param>
        /// <param name="articleID">articleID</param>
        private void LoadImage(string articleImageURL, int articleID)
        {
            if (articleImageURL != string.Empty)
            {
                StringBuilder pathToFile = new StringBuilder();
                pathToFile.Append(AppConfiguration.ArticlePath);
                pathToFile.Append(articleID);
                pathToFile.Append("/");
                pathToFile.Append(articleImageURL);
                this.imgArticleImage.ImageUrl = pathToFile.ToString();
                pathToFile = null;
            }
            else
            {
                this.imgArticleImage.ImageUrl = "image/imgNoArticleImage.jpg";
            }
        }
        /// <summary>
        /// Loads the vehicle details to the hidden fields
        /// </summary>
        /// <param name="articleID">articleID</param>
        private void LoadVehicleDetails(int articleID)
        {
            VehicleModelManager vehicleModelManager = new VehicleModelManager();
            List<GetVehicleModelDetailsByArticleID> vehicleModelDetails = null;
            try
            {
                vehicleModelDetails = vehicleModelManager.GetVehicleModelsByArticleID(articleID);
                if (vehicleModelDetails != null && vehicleModelDetails.Count > 0)
                {
                    this.hdnModelSelectedValue.Value = vehicleModelDetails[0].VehicleModelID.ToString();
                    this.hdnModelTypeSelectedValue.Value = vehicleModelDetails[0].VehicleModelTypeID.ToString();
                    this.hdnModelTypeYearSelectedValue.Value = vehicleModelDetails[0].VehicleModelTypeYearID.ToString();
                }

            }
            catch (Exception)
            {
                this.pageActionMessage.InnerText = "Failed to load vehicle details for article.";
                this.pageActionMessage.Attributes.Add("class", "error-msg");
            }
            finally
            {
                vehicleModelManager = null;
                vehicleModelDetails = null;

            }
        }
        /// <summary>
        /// Loads the article type
        /// </summary>
        /// <param name="articleID">articleID</param>
        private void LoadArticleType(int articleID)
        {
            if (articleID == (int)ArticleEnums.Blog)
            {
                this.lblArticleType.Text = "Blog";
            }
            if (articleID == (int)ArticleEnums.Video)
            {
                this.lblArticleType.Text = "Video";
            }
        }
        #endregion        

        
    }
}